package com.ysp.summary.util;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Objects;

/**
 *使用Java8 的日期写个工具类 玩玩  常用的几个类介绍：
 *
 *  Instant  时间戳                        Duration
 *  LocalDate     只包含日期                        LocalTime  只包含时间
 *  LocalDateTime     包含日期和时间                    Period   时间段
 *  ZoneOffset        时区偏移量                    ZonedDateTime    带时区的时间
 *  Clock   时钟                          DateTimeFormatter  日期格式化
 *
 * @author Administrator
 */
public class DateUtil {

    public static LocalDate getLocalDate(){
    LocalDate localDate = LocalDate.now();
    System.out.println("localDate: "+localDate);
    System.out.println("year: "+localDate.getYear());
    System.out.println("month: "+localDate.getMonth());
    System.out.println("int month: "+localDate.getMonthValue());
    System.out.println("day: "+localDate.getDayOfMonth());
    System.out.println("特定日期: "+LocalDate.of(2021,5,21));
    System.out.println("判断是否相等日期: "+localDate.equals(LocalDate.of(2021,5,21)));
    System.out.println("objects判断是否相等日期: "+ Objects.equals(localDate,LocalDate.of(2021,5,21)));
    System.out.println("一年qian的日期: "+localDate.minus(1, ChronoUnit.YEARS));
    System.out.println("一年后的日期: "+localDate.plus(1,ChronoUnit.YEARS));
    LocalDate date = LocalDate.of(2021,6,26);
    Period period = Period.between(localDate,date);
    System.out.println("日期之间相差天数: "+period.getDays());
    System.out.println("日期之间相差月数: "+period.getMonths());
    System.out.println("日期之间相差年数: "+period.getYears());
    System.out.println("检查是否是闰年: "+localDate.isLeapYear());
    return localDate;
    }

    public static LocalTime getLocalTime(){
        LocalTime local = LocalTime.now();
        System.out.println("LocalTime: "+local);
        System.out.println("三小时后的时间: "+local.plusHours(3));
        System.out.println("三0分钟后的时间: "+local.plusMinutes(30));
        return local;
    }

    public static LocalDateTime getLocalDateTime(){
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime: "+localDateTime);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
        String strTime1 = formatter.format(localDateTime);
        System.out.println("日期格式化1： "+strTime1);
        System.out.println("日期格式化2： "+localDateTime.format(formatter));
        LocalDateTime dateStr = LocalDateTime.parse(strTime1,formatter);
        System.out.println("字符串转日期： "+dateStr);
        return localDateTime;
    }

    public static long getTime(){
        YearMonth currentYearMonth = YearMonth.now();
        System.out.printf("当前年月 和当月天数： %s : %d天%n",currentYearMonth,currentYearMonth.lengthOfMonth());
        YearMonth yearMonth = YearMonth.of(2021,07);
        YearMonth yearMonth2 = YearMonth.of(2021,Month.DECEMBER);
        System.out.printf("指定当前年月： %s %n",currentYearMonth);
        Clock clock = Clock.systemDefaultZone();
        System.out.println("clock获取当前时间戳： "+clock.millis());
        Clock clock2 = Clock.systemUTC();
        System.out.println("clock获取当前时间戳: "+clock2.millis());
        Instant timeStamp = Instant.now();
        System.out.println("Instant获取当前时间戳: "+timeStamp.toEpochMilli());
        return clock2.millis();
    }

  public static void main(String[] args) {
      DateUtil.getLocalDate();
      DateUtil.getLocalTime();
      DateUtil.getLocalDateTime();
      DateUtil.getTime();
      System.nanoTime();
  }
}
